package cn.cscb.mms.server.controller;


import cn.cscb.mms.common.context.BaseContext;
import cn.cscb.mms.common.result.Result;
import cn.cscb.mms.common.result.ResultEnum;
import cn.cscb.mms.pojo.entity.*;
import cn.cscb.mms.server.service.TaskInfoService;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/taskInfo")
public class TaskInfoController {

    @Resource
    private TaskInfoService taskInfoService;

    @PostMapping("/selectTaskList")
    public Result<IPage<TaskInfoVO>> selectTaskList(@RequestBody TaskInfoParam taskInfoParam){
        IPage<TaskInfoVO> p = taskInfoService.selectTaskList(taskInfoParam);
        return Result.success(p);

    }

    @PostMapping("/addTaskList")
    public Result<TaskInfo> addTaskList(@RequestBody TaskInfo taskInfo){
        taskInfo.setCreateId(BaseContext.getCurrentEmpNo());
        return Result.success("新增成功！", taskInfoService.addTaskList(taskInfo));
    }

    @PostMapping("/updateTaskList")
    public Result<TaskInfo> updateTaskList(@RequestBody UpdateTaskInfo updateTaskInfo){
        TaskInfo taskInfo = new TaskInfo();

        taskInfo.setMarkNo(updateTaskInfo.getMarkNo());
        taskInfo.setMarkName(updateTaskInfo.getMarkName());
        taskInfo.setMarkType(updateTaskInfo.getMarkType());
        taskInfo.setCusId(updateTaskInfo.getCusId());
        taskInfo.setReward(updateTaskInfo.getReward());
        taskInfo.setDescription(updateTaskInfo.getDescription());

        Integer flag = updateTaskInfo.getFlag();
        return Result.success("更新成功！", taskInfoService.updateTask(taskInfo, flag));
    }

    @PostMapping("/deleteTaskList")
    public Result deleteTaskList(@RequestParam String markNo){
        Boolean idDelete = taskInfoService.deleteTask(markNo);
        String message = "";
        if(idDelete) {
            message = "删除成功！";
            return Result.success(message,idDelete);
        }
        else {
            message = "该商机已被认领，删除失败！";
            return Result.success(message, idDelete);
        }
    }

    @PostMapping("/deleteManyTaskList")
    public Result deleteManyTaskList(@RequestBody List<String> markNoList){
        taskInfoService.deleteTaskList(markNoList);

        String message = "删除成功！";
        return Result.success(message);
    }


    /**
     * 派发商机
     * @param
     * @return
     */
    @PostMapping("/distribution")
    public Result<Boolean> distribution(@RequestBody TaskInfo taskInfo){
        taskInfo.setDispId(BaseContext.getCurrentEmpNo());
        if(StringUtils.isBlank(taskInfo.getMarkNo())){
            return Result.error("商机信息有误，派发失败！");
        }
        if (StringUtils.isBlank(taskInfo.getDispId())){
            return Result.error("未选择派发客户经理，派发失败！");
        }
        if (StringUtils.isBlank(taskInfo.getClaimId())){
            return Result.error("派发管理者信息有误，派发失败！");
        }
        return Result.success("派发成功！", taskInfoService.distribution(taskInfo.getMarkNo(), taskInfo.getDispId(), taskInfo.getClaimId()));
    }

    /**
     * 认领商机
     * @param
     * @return
     */
    @PostMapping("/claim")
    public Result<Boolean> claim(@RequestBody TaskInfo taskInfo){

        taskInfo.setClaimId(BaseContext.getCurrentEmpNo());
        if(StringUtils.isBlank(taskInfo.getMarkNo())){
            return Result.error("商机信息有误，认领失败！");
        }
        if (StringUtils.isBlank(taskInfo.getClaimId())){
            return Result.error("客户经理信息有误，认领失败！");
        }
        return Result.success("认领成功！", taskInfoService.claim(taskInfo.getMarkNo(), taskInfo.getClaimId()));
    }

    @GetMapping("/getAllList")
    public Result<IPage<TaskInfo>> getAllList(@RequestBody TaskInfoParam taskInfoParam){
        return Result.success(taskInfoService.selectList(taskInfoParam.getPage(), taskInfoParam.getPageSize()));
    }

    @GetMapping("/getMarkStatus")
    public Result<List<MarkStatus>> getMarkStatus(){
        return Result.success(taskInfoService.getMarkStatus());
    }

    @GetMapping("/getMarkType")
    public Result<List<MarkType>> getMarkType(){
        return Result.success(taskInfoService.getMarkType());
    }

    @GetMapping("/getORG")
    public Result<List<Map>> getORG(){
        return Result.success(taskInfoService.getORG());
    }

    @PostMapping("/getAccountManager")
    public Result<List<Map>> getAccountManager(@RequestParam("org") String org){
        return Result.success(taskInfoService.getAccountManager(org));
    }

    @GetMapping("/export")
    public Result<Boolean> export(HttpServletResponse response) throws Exception {

        return Result.success("导出成功！", taskInfoService.export(response));

    }

    @PostMapping("/import")
    public Result<Boolean> imp(MultipartFile file) throws Exception {
        return Result.success("导入成功！", taskInfoService.imp(file));
    }

    @GetMapping("/getCus")
    public Result<List<Map>> getCus(){
        return Result.success(taskInfoService.getCus());
    }

    @GetMapping("/isManager")
    public Result<Boolean> isManager(){
        return Result.success(taskInfoService.isManager(BaseContext.getCurrentEmpNo()));
    }

}
